// pages/searchResult/searchResult.js
let douban = require("../../common/script/fetch")
let config = require("../../common/script/config")
Page({
  data: {
    films: [],
		hasMore: true,
		showLoading: true,
		start: 0,
		url: "",
		keyword: "",
		isNull: false,
		nullTip: {
			tipText: "sorry，没有找到您要的内容，换个关键词试试吧",
			actionText: "返回",
			routeUrl: "../search/search"
		}
  },
  onLoad: function (options) {
    let that = this
    that.setData({
      url: options.url,
      keyword: options.keyword,
      title: options.keyword
    })
    douban.search.call(that, that.data.url, that.data.keyword, that.data.start, config.count, function(data) {
      if (data.subjects.length == 0) {
				that.setData({
					isNull: true
				})
			}
    })
  },
  onPullDownRefresh: function() {
    let that = this
		that.setData({
			films: [],
			hasMore: true,
			showLoading: true,
			start: 0
		})
		douban.search.call(that, that.data.url, that.data.keyword, that.data.start, config.count)
  },
  onReachBottom: function() {
    let that = this
		if (!that.data.showLoading) {
			douban.search.call(that, that.data.url, that.data.keyword, that.data.start, config.count)
		}
  },
  viewFilmDetail: function(e) {
		let data = e.currentTarget.dataset;
		wx.redirectTo({
			url: "../filmDetail/filmDetail?id=" + data.id
		})
	},
	viewFilmByTag: function(e) {
		let data = e.currentTarget.dataset
		let keyword = data.tag
		wx.redirectTo({
			url: "../searchResult/searchResult?url=" + encodeURIComponent(config.apiList.search.byTag) + "&keyword=" + keyword
		})
	}
})